package com.atguigu.gulimall.coupon.service.impl;

import com.atguigu.common.to.MemberPrice;
import com.atguigu.common.to.SkuReductionTo;
import com.atguigu.common.to.SpuBoundTo;
import com.atguigu.gulimall.coupon.entity.MemberPriceEntity;
import com.atguigu.gulimall.coupon.entity.SkuLadderEntity;
import com.atguigu.gulimall.coupon.service.MemberPriceService;
import com.atguigu.gulimall.coupon.service.SkuLadderService;
import com.atguigu.gulimall.coupon.service.SpuBoundsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.coupon.dao.SkuFullReductionDao;
import com.atguigu.gulimall.coupon.entity.SkuFullReductionEntity;
import com.atguigu.gulimall.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

//    保存我们保存商品的时候全部的优惠信息 以及 会员价格等等
    @Transactional // 这个也要操作多张表
    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        if(skuReductionTo.getFullCount() > 0) {
            // 5.4)、sku的优惠信息和满减信息 gulimall_sms -> sms_sku_ladder  这个是打折表几件打几折
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
            skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
            // 打几折
            skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
            // 状态是否参与其他优惠
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());

            // 最终打折后是多少钱
            //        skuLadderEntity.setPrice(skuReductionTo);
            skuLadderService.save(skuLadderEntity);
        }

        // 这里也单独进行判断
        if ((skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1)) {
            // 5.5)、sku的满减表  满多少钱减多少  gulimall_sms -> sms_sku_full_reduction
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            skuFullReductionEntity.setSkuId(skuReductionTo.getSkuId());
            // 满多少
            skuFullReductionEntity.setFullPrice(skuReductionTo.getFullPrice());
            // 减多少钱
            skuFullReductionEntity.setReducePrice(skuReductionTo.getReducePrice());
            // 建了是否叠加
            skuFullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());
            save(skuFullReductionEntity);

        }


        // 5.6)、sku对应会员等级所购买的价格 gulimall_sms -> sms_member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();

        if(memberPrice != null && memberPrice.size() > 0) {


            List<MemberPriceEntity> priceEntities = memberPrice.stream().map((item) -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setAddOther(1); // 默认就是叠加优惠
                return memberPriceEntity;
            }).filter((entity) -> {
                // 只留下我们设置了会员价钱的数据
                return (entity.getMemberPrice().compareTo(new BigDecimal("0")) == 1);
            }).collect(Collectors.toList());

            // 最终进行批量保存
            memberPriceService.saveBatch(priceEntities);
        }
    }

}